package vaani.swar.grammar;

import java.io.IOException;
import java.util.List;
import javax.speech.recognition.GrammarException;
import javax.speech.recognition.RuleGrammar;
import vaani.internal.purple.Buddy;
import vaani.internal.purple.BuddyListHandler;

/**
 *
 * @author sourcemorph
 */
public class InstantMessageGrammarConstructor extends GenericGrammarConstructor {

    /*method to construct the grammar dynamically*/
    public static void constructGrammar() {
        try {
            jsgfGrammarManager.loadJSGF("instantmessage");
            RuleGrammar ruleGrammar = jsgfGrammarManager.getRuleGrammar();
            addRule(ruleGrammar, "start", "I (want|wish) [you] to | let me");
            addRule(ruleGrammar, "action", "chat [with] | [private] message");
            addRule(ruleGrammar, "extra", "on <protocol>");
            /*the dynamic parts*/
            addRule(ruleGrammar, "protocol", getProtocolRule());
            addRule(ruleGrammar, "buddy", getBuddyRule());
            addRule(ruleGrammar, "complete", "[<start>] <action> <buddy> [<extra>]");
        } catch(IOException ex) {
            ex.printStackTrace();
        } catch(GrammarException ex) {
            ex.printStackTrace();
        }
        
    }

    private static String getBuddyRule() {
        String buddyRule = null;
        List<Buddy> buddyList = BuddyListHandler.getAllBuddies();
        for(Buddy b : buddyList) {
            if(buddyRule == null) {
                buddyRule = b.getAlias();
            } else {
                buddyRule += " | " + b.getAlias();
            }
        }
        return buddyRule;
    }

    private static String getProtocolRule() {
        String protocolRule = null;
        List<String> protocolList = BuddyListHandler.getAllProtocols();
        for(String protocol : protocolList) {
            if(protocolRule == null) {
                protocolRule = protocol;
            }
            else {
                protocolRule += " | " + protocol;
            }
        }
        return protocolRule;
    }
}
